﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ParallelDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            var inputs = Enumerable.Range(1, 20).ToArray();
            var outputs = new int[inputs.Length];

            var sw = Stopwatch.StartNew();

            //for (int i = 0; i < inputs.Length; i++)
            //{
            //    outputs[i] = HeavyJob(inputs[i]);
            //}

            //Parallel.For(0, inputs.Length, i => outputs[i] = HeavyJob(inputs[i]));
            var tmps=  inputs.Select(x => x + 10).ToArray();
            outputs =inputs.AsParallel().AsOrdered().Select(x => HeavyJob(x)).ToArray();
            Console.WriteLine($"Elapsed time：{sw.ElapsedMilliseconds}ms");

            PrintArr(outputs);
            Console.ReadKey();

        }

        static int HeavyJob(int input)
        {
            Thread.Sleep(1000);
            return input * input;
        }

        static void PrintArr(int [] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine(arr[i]);
            }
        }
    }
}
